package org.budo.dubbo.protocol.http.invocation;

import org.budo.support.lang.util.StringUtil;

import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.RpcInvocation;

import lombok.Getter;
import lombok.Setter;

/**
 * 扩展了 RpcInvocation
 * 
 * @author lmw
 */
@Getter
@Setter
public class BudoDubboRpcInvocation extends RpcInvocation {
    private static final long serialVersionUID = 6371604131786981852L;

    public static final String BUDO_INVOCATION_IN_REQUEST_ATTRIBUTE_KEY = "BUDO_INVOCATION_IN_REQUEST_ATTRIBUTE_KEY";

    private transient String interfaceName;

    /**
     * TODO requestBody 不用 String 存储，用 byte[]
     */
    private transient String requestBody;

    private transient Boolean responseBody;

    public BudoDubboRpcInvocation() {
    }

    public BudoDubboRpcInvocation(Invocation invocation, String requestBody) {
        super(invocation);

        this.setRequestBody(requestBody);
    }

    public String interfaceName() {
        if (null != this.interfaceName) {
            return this.interfaceName;
        }

        Class<?> interfaceType = this.getInvoker().getInterface();
        return interfaceType.getName();
    }

    public String toString() {
        String str = this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
        str += ", super=" + super.toString();
        str += ", interface=" + (null == this.getInvoker() ? "invoker null" : this.getInvoker().getInterface());
        str += ", requestBody=" + StringUtil.cutLeft(this.getRequestBody(), 1024);
        return str;
    }
}
